package com.example.mystore.dao;
import com.example.mystore.entity.Order;
import com.example.mystore.utils.LinkMySQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Objects;

public class OrderDao {
    public  static boolean insertOrder(Order order){
        Connection conn = null;
        PreparedStatement pstmt = null;
        int numb = 0;
        try {
            conn = LinkMySQL.getConnection();
            String sql = "insert into orders(customerEmail,commodityId,commodityNumber,consigneeName,consigneeAddress,consigneePAC,consigneePhone,consigneeFax,cardType,carName,cardNumber,billAddress,closingDate,taxRate,freight,commodityPrice)" +
                    " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            pstmt = Objects.requireNonNull(conn).prepareStatement(sql);
            pstmt.setString(1,order.getCustomerEmail());
            pstmt.setInt(2,order.getCommodityId());
            pstmt.setInt(3,order.getCommodityNumber());
            pstmt.setString(4,order.getConsigneeName());
            pstmt.setString(5,order.getConsigneeAddress());
            pstmt.setString(6,order.getConsigneePAC());
            pstmt.setString(7,order.getConsigneePhone());
            pstmt.setString(8,order.getConsigneeFax());
            pstmt.setString(9,order.getCardType());
            pstmt.setString(10,order.getCarName());
            pstmt.setString(11,order.getCardNumber());
            pstmt.setString(12,order.getBillAddress());
            pstmt.setString(13,order.getClosingDate());
            pstmt.setDouble(14,order.getTaxRate());
            pstmt.setDouble(15,order.getFreight());
            pstmt.setDouble(16,order.getPrice());
            numb = pstmt.executeUpdate();
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            LinkMySQL.release(pstmt,conn);
        }
        return numb > 0;
    }
}
